//
//  NavigationC.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/10.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import UIKit

class NavigationC: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.delegate = NavigationCDelegate.shared
        /// 手势
        guard let gestureRecognizer = self.interactivePopGestureRecognizer else {
            return
        }
        gestureRecognizer.delegate = self
        
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        /// ios11调整navigationBar上按钮的左右间距,默认左右各20
        self.adjustNavigationBarItemsSpacing()
    }
    
    /// ios11调整navigationBar上按钮的左右间距,默认左右各20
    private func adjustNavigationBarItemsSpacing() {
        if #available(iOS 11, *) {
            for subview in self.navigationBar.subviews {
                if String(describing: subview).contains("UINavigationBarContentView") {
                    for constant in subview.constraints {
                        if constant.constant == 20 || constant.constant == -20 {
                            constant.constant = 0
                        }
                    }
                }
            }
        }
    }
    
    
    // MARK: - pushViewController
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if let gestureRecognizer = self.interactivePopGestureRecognizer {
            gestureRecognizer.isEnabled = false
        }
        if self.viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
            /// 左侧返回按钮
//            var back = UIBarButtonItem(image: theme.navbarTheme.backImage, style: .done, target: self, action: #selector(self.back))
//            if let vc = viewController as? CPVCPopBackAction {
//                back = UIBarButtonItem(image: theme.navbarTheme.backImage, style: .done, target: vc, action: #selector(vc.backAction))
//            }
//            viewController.navigationItem.leftBarButtonItems = [back]
        }
        super.pushViewController(viewController, animated: animated)
    }
}

extension NavigationC: UIGestureRecognizerDelegate { }


class NavigationCDelegate: NSObject, UINavigationControllerDelegate {
    
    static let shared = NavigationCDelegate()
    
    private override init(){}
    
    func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        guard let gestureRecognizer = navigationController.interactivePopGestureRecognizer else {
            return
        }
        gestureRecognizer.isEnabled = !(navigationController.viewControllers.count == 1)
    }
    
    func navigationController(_ navigationController: UINavigationController, willShow viewController: UIViewController, animated: Bool) {
        viewController.navigationController?.navigationBar.alpha = 1
        viewController.navigationController?.navigationBar.tintColor = nil
        viewController.navigationController?.navigationBar.isTranslucent = false
    }
    
}
